<?php

namespace App\Admin\Controllers;

use App\Agency;
use App\Expert;
use App\Extensions\ContentSingle;
use App\Extensions\LayerForm;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;

class AgencyController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '合作代理机构';
    protected $role_type = 1;

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Agency());
        $grid->model()->where('role_type', 4)->orderBy('order')->orderByDesc('id');
        $grid->disableExport();
        if ($this->role_type == 1) {
            $grid->column('username', '用户名');
        } else {
            $grid->disableActions();
            $grid->disableCreateButton();
        }
        $grid->column('company', __('代理机构'));
        $grid->column('name', __('负责人'));
        $grid->column('mobile', __('电话'));
        $grid->column('about', __('机构简介'))->display(function ($val) {
            return $val ? '查看' : '';
        })->modal('机构简介', function ($model) {
            return $model->about;
        });
        $grid->column('major', __('机构优势领域'));
        $grid->column('data->fm_publish', __('发明公布'));
        $grid->column('data->fm_sq', __('发明授权'));
        $grid->column('data->xx_sq', __('实用新型授权'));
        $grid->column('data->wg_sq', __('外观设计授权'));
        $grid->column('data1', __('合计授权'))->display(function ($val) {
            return collect($this->data)->only(['fm_sq', 'xx_sq', 'wg_sq'])->values()->filter()->sum() ?: '';
        });
        $grid->column('address', __('地址'));
        $grid->column('created_at', __('Created at'));
        $grid->hideColumns(['address', 'created_at']);
        $grid->filter(function (Grid\Filter $filter) {
            $filter->disableIdFilter();
            $filter->column(1 / 2, function (Grid\Filter $filter) {
                $filter->like('company', '代理机构');
                $filter->like('name', '负责人');
            });
            $filter->column(1 / 2, function (Grid\Filter $filter) {
                $filter->like('phone', '电话');
                $filter->like('address', '地址');
            });
        });
        $grid->quickSearch('name', 'company', 'mobile')->placeholder('代理机构/负责人/电话');
        //$grid->quickSearch('name', 'agent','phone','address');
        $grid->batchActions(function (Grid\Tools\BatchActions $actions) {
            $actions->disableDeleteAndHodeSelectAll();
        });
        $grid->disableFilter();
        return $grid;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $user = Admin::user();
        $form = new Form(new Agency());
        $form->text('username', trans('admin.username'))
            ->creationRules(['required', "unique:admin_users,username"], ['required' => '账号不能为空', 'unique' => '账号不能重复'])
            ->updateRules(['required', "unique:admin_users,username,{{id}}"], ['required' => '账号不能为空', 'unique' => '账号不能重复']);
        $form->password('password', trans('admin.password'))->rules('required|confirmed');
        $form->password('password_confirmation', trans('admin.password_confirmation'))->rules('required')
            ->default(function ($form) {
                return $form->model()->password;
            });
        $form->text('company', __('代理机构'))->required();
        $form->text('name', __('负责人'))->required();
        $form->mobile('mobile', __('电话'))->required();
        $form->textarea('major', __('机构优势领域'));
        $form->textarea('about', __('机构简介'));
        $form->text('address', __('地址'));
        $states = [
            'on'  => ['value' => 1, 'text' => '是', 'color' => 'success'],
            'off' => ['value' => 0, 'text' => '否', 'color' => 'danger'],
        ];
        $form->switch('enabled', '启用')->states($states)->default(1);

        if ($user->id == 1) {
            $form->embeds('data', '专利统计', function ($form) {
                $form->number('fm_publish', '发明公开');
                $form->number('fm_sq', '发明授权');
                $form->number('xx_sq', '实用新型授权');
                $form->number('wg_sq', '外观设计授权');
            });
        }
        $form->ignore(['password_confirmation']);
        if ($form->isCreating()) {
            $form->hidden('role_type');
        }
        $form->saving(function (Form $form) {
            $form->role_type = 4;
            if ($form->password && $form->model()->password != $form->password) {
                $form->password = bcrypt($form->password);
            }
        });
        return $form;
    }
}
